510D - Fox And Jumping - CodeForces Solution


bitmasks brute force dp math *1900

Please click on ads to support us..

C++ Code:

#define fst std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout << std::fixed << std::setprecision(20)
#define le "\n"
#define ll long long 
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+50;
const int mod=998244353;
map<ll,ll> mp;

ll gcd(ll n,ll m){
    return m? gcd(m,n%m): n;
}

int main() {
    int n; cin>>n;
    vector<ll> a(n+1),w(n+1);
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>w[i];

    for(int i=1;i<=n;i++){
        mp[a[i]] = mp[a[i]] ? min(mp[a[i]],w[i]) : w[i]; 
        for(auto j: mp){
            mp[gcd(j.first,a[i])] = mp[gcd(j.first,a[i])] ? min(mp[gcd(j.first,a[i])],j.second+w[i]) : j.second+w[i];
        }
    }
    cout<<(mp[1] ? mp[1]:-1)<<le;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game